package org.harryrose.resultkit;

import java.io.File;

/**
 * The ResultKit class, except with a number of buffers that can be used for state saves.  This can
 * be used to ensure that if the simulation is killed part-way through writing the state, the state from
 * the previous iteration is uncorrupted and consistent. 
 * @author Harry Rose
 */
public class ResultKitWithMultibufferedState extends ResultKit{

	final int noStateBuffers;
	int currentStateBuffer = 0;
	
	/**
	 * Creates a ResultKit instance that stores results in resultDirectory and has a number of separate 
	 * folders in which to store states. 
	 * @param resultDirectory  Directory in which to store data.
	 * @param noStateBuffers Number of state buffers to use.
	 */
	public ResultKitWithMultibufferedState(File resultDirectory, int noStateBuffers) {
		this(resultDirectory.toString(),noStateBuffers);
	}
	
	/**
	 * Creates a ResultKit instance that stores results in resultDirectory and has a number of separate 
	 * folders in which to store states. 
	 * @param resultDirectory  Directory in which to store data.
	 * @param noStateBuffers Number of state buffers to use.
	 */
	public ResultKitWithMultibufferedState(String resultDirectory, int noStateBuffers) {
		super(resultDirectory);
		noStateBuffers = (noStateBuffers < 1) ? 2 : noStateBuffers;
		this.noStateBuffers = noStateBuffers;
	}

	@Override
	public File getStateDirectory()
	{
		File stateDir = new File(super.getStateDirectory().toString(), currentStateBuffer+"");
		return stateDir;
	}
	
	/**
	 * Advances the buffer.
	 * @return
	 */
	public int changeToNextStateBuffer()
	{
		return currentStateBuffer = ((1+currentStateBuffer) % noStateBuffers);
	}
}
